<?php

namespace api\modules\v1\controllers;

use api\common\controllers\ApiController;

use yii\filters\AccessControl;
use yii;
use api\modules\v1\models\LoginForm;
use api\modules\v1\models\AdminUser;
use yii\base\DynamicModel;

/**
 * Public controller for the `user` module
 */
class AdminUserController extends ApiController
{
 //所有验证都不需要access-token
    public function behaviors ()
    {
        return [
            'access' => [
                'class' => AccessControl::className() ,
                'rules' => [
                    [
                        'allow'   => TRUE ,
                        'actions' => [] ,
                        'roles'   => [ '?' ] ,
                    ] ,
                ] ,
            ] ,
        ];
    }
    //
    public function actionLogin()
    {
        if ( !Yii::$app->user->isGuest ) {
            return $this->goHome();
        }
        $model = new LoginForm();
        $time = 3600 * 3; //登录之后3小时免登陆
        if ( $model->load( Yii::$app->request->post(),'' ) && $model->login($time) ) {

            $user_info = AdminUser::findOne( Yii::$app->user->identity->getId() );
            $user_info->access_token = sha1(Yii::$app->request->post('username').Yii::$app->request->post('password').date('Y-m-d H:i:s', time()).parent::str_rand(6));
            $user_info->scenario = 'logout';
            $user_info->last_time = date('Y-m-d H:i:s',time());
            $user_info->last_ip = Yii::$app->request->userIP;
            $user_info->save( FALSE );
            $data = $user_info->access_token;
            return parent::isMessage(200,'登陆成功',$data);
        }else{
            return parent::isMessage(400,'用户名或密码不正确');
        }
    }


}
